package com.insurance.security;

import com.insurance.entity.AuthenticationUserEntity;
import com.insurance.service.AuthenticationUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author Style
 * @version 1.0
 * @date 2020/12/2
 **/
@Component
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private AuthenticationUserService authenticationUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AuthenticationUserEntity authenticationUser = authenticationUserService.findByUserName(username);
        return new UserInfo(String.valueOf(authenticationUser.getId()), authenticationUser.getUserName(), authenticationUser.getPassword(), "");
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

}
